//
//  LXWithdrawalRecordView.m
//  lixing
//
//  Created by bancheng on 2022/6/13.
//

#import "LXAppointmemtListView.h"
#import "LXAppointmentTableViewCell.h"
#import "LXAppointmentModel.h"

@interface LXAppointmemtListView()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic) UITableView *tableView;

@property(nonatomic) NSInteger page;
// 已预约
@property(nonatomic) NSInteger yiBook_count;
// 待确认
@property(nonatomic) NSInteger daiBook_count;
// 已取消
@property(nonatomic) NSInteger cancel_count;
// 全部
@property(nonatomic) NSInteger all_count;

@property(nonatomic) NSMutableArray *counts;

@end

@implementation LXAppointmemtListView

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self addSubview:self.tableView];
        
        self.data = [NSMutableArray array];
        self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            self.page = 1;
            [self loadData];
            
        }];
        [self.tableView.mj_header beginRefreshing];
        self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            self.page++;
            [self loadData];
        }];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadData) name:@"sendMessageNotification" object:nil];

    }
    return self;
}

-(void)loadData{

    NSDictionary *dic = @{@"page":@(self.page),@"rows":@(10),@"status":[NSString stringWithFormat:@"%@",@(self.selectIndex)]};
    [MSNetworkManager getRevertUrl:[DOMAIN_URL stringByAppendingString:GET_GetTappointmentPage] params:dic success:^(id  _Nonnull paginationObj, id  _Nonnull resonseObj, BOOL isSuccess, NSString * _Nonnull message) {
        [self endRefreshing];
        
        if (isSuccess) {
            if (self.page == 1 || [self.tableView.mj_header isRefreshing]) {
                [self.data removeAllObjects];
                self.tableView.mj_header.state = MJRefreshStateIdle;
                [self.tableView.mj_footer resetNoMoreData];
            }
            
           
            if (!ObjectIsEmpty(self.counts)) [self.counts removeAllObjects];
            /**0 全部 1 已预约  2待确认 3 已取消  4 已签约 */
            NSDictionary *dic;
            if([resonseObj[@"status"] intValue] == 0){
                dic = @{
                    @"title": [NSString stringWithFormat:@"全部(%@)",@([resonseObj[@"total"] intValue])],
                    @"status":@([resonseObj[@"status"] intValue])
                };
            }
            else if([resonseObj[@"status"] intValue] == 1){
                dic = @{
                    @"title": [NSString stringWithFormat:@"已预约(%@)",@([resonseObj[@"total"] intValue])],
                    @"status":@([resonseObj[@"status"] intValue])
                };

            }
            else if([resonseObj[@"status"] intValue] == 2){
                dic = @{
                    @"title": [NSString stringWithFormat:@"待确认(%@)",@([resonseObj[@"total"] intValue])],
                    @"status":@([resonseObj[@"status"] intValue])
                };

            }
            else if([resonseObj[@"status"] intValue] == 3){
                dic = @{
                    @"title": [NSString stringWithFormat:@"已取消(%@)",@([resonseObj[@"total"] intValue])],
                    @"status":@([resonseObj[@"status"] intValue])
                };

            }
            
            [[NSNotificationCenter defaultCenter] postNotificationName:@"setUnCountNoti" object:dic];
            
            
            NSArray *array = resonseObj[@"records"];
           
            [self.data addObjectsFromArray:[LXAppointmentModel mj_objectArrayWithKeyValuesArray:array]];

            if (array.count < 1) {
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }
            [self isHaveEmptyData];
            [self.tableView reloadData];
        }
        
    } failure:^(NSUInteger code, NSString * _Nonnull message) {
        [self isHaveEmptyData];
        [self endRefreshing];
    } withShowHud:NO];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.data.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  
    LXAppointmentModel *model = self.data[indexPath.row];
    LXAppointmentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([LXAppointmentTableViewCell class]) forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    cell.model = model;
    
    [cell.callPhone whenTapped:^{
//        NSMutableString * str=[[NSMutableString alloc] initWithFormat:@"telprompt://%@",model.userPhone];
//        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str] options:@{} completionHandler:nil];
    }];
    
    return cell;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([self.delegate respondsToSelector:@selector(didselectRowWithModel:)]) {
        LXAppointmentModel *model = self.data[indexPath.row];
        [self.delegate didselectRowWithModel:model];
        
    }
}
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height) style:(UITableViewStylePlain)];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor colorWithHexString:@"#F7F8FA"];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([LXAppointmentTableViewCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([LXAppointmentTableViewCell class])];
        _tableView.tableFooterView = [UIView new];
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        } else {}
    }
    return _tableView;
}
- (void)endRefreshing{
    if ([self.tableView.mj_header isRefreshing]) {
        [self.tableView.mj_header endRefreshing];
    }
    if ([self.tableView.mj_footer isRefreshing]) {
        [self.tableView.mj_footer endRefreshing];
    }

}
- (void)isHaveEmptyData{
    if (self.data.count == 0) {
        self.tableView.startTip = YES;
        self.tableView.tipView.backgroundColor = self.tableView.backgroundColor;
        self.tableView.tipImage = [UIImage imageNamed:@"No_order_icon"];
        self.tableView.tipView.hidden = NO;
        self.tableView.scrollEnabled = YES;
    }else{
        self.tableView.tipView.hidden = YES;
        self.tableView.scrollEnabled = YES;
    }
}



@end
